package com.example.auth.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.auth.common.R;
import com.example.auth.model.dto.UserAddDTO;
import com.example.auth.model.dto.UserUpdateDTO;
import com.example.auth.model.entity.SysUser;
import com.example.auth.model.vo.SysUserVo;
import com.example.auth.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

/**
 * 用户管理控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private SysUserService userService;

    /**
     * 获取用户分页列表
     */
    @GetMapping("/page")
    @SaCheckPermission("user:list")
    public R<Page<SysUser>> getUserPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String nickname,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        
        Page<SysUser> page = userService.getUserPage(current, size, username, nickname, phone, startDate, endDate);
        return R.ok(page);
    }

    /**
     * 根据部门ID获取用户列表
     */
    @GetMapping("/dept/{deptId}")
    @SaCheckPermission("user:list")
    public R<List<SysUser>> getUsersByDeptId(@PathVariable Long deptId) {
        List<SysUser> list = userService.getUsersByDeptId(deptId);
        return R.ok(list);
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/{id}")
    @SaCheckPermission("user:list")
    public R<SysUserVo> getUserById(@PathVariable Long id) {
        SysUserVo user = userService.getUserVoById(id);
        return R.ok(user);
    }

    /**
     * 新增用户
     */
    @PostMapping
    @SaCheckPermission("user:add")
    public R<Boolean> addUser(@RequestBody @Valid UserAddDTO userDTO) {
        boolean result = userService.addUser(userDTO);
        return R.ok(result);
    }

    /**
     * 更新用户信息
     */
    @PutMapping
    @SaCheckPermission("user:update")
    public R<Boolean> updateUser(@RequestBody @Valid UserUpdateDTO userDTO) {
        boolean result = userService.updateUser(userDTO);
        return R.ok(result);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("user:delete")
    public R<Boolean> deleteUser(@PathVariable Long id) {
        boolean result = userService.removeById(id);
        return R.ok(result);
    }
    
    /**
     * 重置用户密码
     */
    @PutMapping("/resetPassword/{id}")
    @SaCheckPermission("user:update")
    public R<Boolean> resetPassword(@PathVariable Long id) {
        boolean result = userService.resetPassword(id);
        return R.ok(result);
    }
    
    /**
     * 修改用户状态
     */
    @PutMapping("/status/{id}/{status}")
    @SaCheckPermission("user:update")
    public R<Boolean> updateUserStatus(@PathVariable Long id, @PathVariable Integer status) {
        if (status != 0 && status != 1) {
            return R.failed("状态值不正确");
        }
        boolean result = userService.updateUserStatus(id, status);
        return R.ok(result);
    }
    
    /**
     * 分配用户角色
     */
    @PutMapping("/assignRoles/{userId}")
    @SaCheckRole("ADMIN")
    public R<Boolean> assignRoles(@PathVariable Long userId, @RequestBody List<Long> roleIds) {
        boolean result = userService.assignRoles(userId, roleIds);
        return R.ok(result);
    }
    
    /**
     * 获取用户角色ID列表
     */
    @GetMapping("/roleIds/{userId}")
    @SaCheckPermission("user:list")
    public R<List<Long>> getUserRoleIds(@PathVariable Long userId) {
        List<Long> roleIds = userService.getUserRoleIds(userId);
        return R.ok(roleIds);
    }
} 